﻿using System;
using System.Web.UI;
using mojoPortal.Web.Framework;

namespace ud_SwirlingQuestion.SwirlingQuestion.Controls
{
    public partial class SearchControl : UserControl
    {
        public string SiteRoot { get; set; }
        public int PageId { get; set; }
        public int ModuleId { get; set; }
        private SwirlingQuestionConfiguration config = new SwirlingQuestionConfiguration();
        public SwirlingQuestionConfiguration Config { get { return config; } set { config = value; } }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += Page_Load;
            btnSearch.Click += btnSearch_Click;
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            if (txtSearchInput.Text.Trim() == Config.SearchWatermarkTextSettings)
            {
                txtSearchInput.Text = string.Empty;
            }
            Session["qasearchterm"] = txtSearchInput.Text.Trim();

            WebUtils.SetupRedirect(this, SiteRoot + "/SwirlingQuestion/SearchResult.aspx?pageid=" + PageId + "&mid=" + ModuleId);
        }

        void Page_Load(object sender, EventArgs e)
        {
            PopulateLabels();
        }

        private void PopulateLabels()
        {
            btnSearch.Text = config.SearchButtonTextSettings;
            txtSearchInput.Watermark = config.SearchWatermarkTextSettings;
        }
    }
}